<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitNewLottery extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('lottery_activity', function (Blueprint $table) {

            $table->comment = '抽奖活动表';

            $table->increments('id');
            $table->uuid('uuid')->index() ;

            $table->string('title', 255)->nullable()->comment('标题'); // 标题
            $table->string('sub_title', 255)->nullable()->comment('副标题'); // 副标题

            $table->string('thumb', 255)->nullable()->comment('封面'); // 封面
            $table->json('images')->nullable()->comment('轮播图');
            $table->json('detail_images')->nullable()->comment('详情图');

            $table->char('emit_type', 20)->nullable()->comment('time为按时间, user_total为按人数, ticket_total为按签号数量开奖');

            $table->timestamp('emit_time')->nullable()->index()->comment('触发开奖时间');
            $table->integer('emit_user_total')->nullable()->comment('触发的参与人数');
            $table->integer('emit_ticket_total')->nullable()->comment('触发的签号数量');

            $table->integer('money_price')->index()->nullable()->comment('人民币价格'); // 支付价格
            $table->integer('score_price')->index()->nullable()->comment('积分价格'); // 支付价格

            $table->integer('visit_total')->default(0)->comment('访问人数'); // 访问人数

            $table->integer('session_total')->default(1)->comment('总期数'); // 
            $table->integer('current_session')->default(1)->comment('当前期数'); // 

            $table->integer('prize_total')->default(0)->comment('奖品份数'); // 活动总件数
            $table->integer('stock')->nullable()->comment('剩余库存'); // 剩余库存
            $table->integer('sales')->default(0)->comment('销量'); 

            $table->tinyInteger('old_user_reward_ticket')->default(1)->comment('邀请老用户注册可获得抽奖码数量'); // 剩余库存
            $table->tinyInteger('new_user_reward_ticket')->default(2)->comment('邀请新用户注册可获得抽奖码数量'); 
            $table->boolean('is_need_phone')->default(0)->comment('是否强制要求绑定手机号后可参与'); // none为免费 

            $table->json('join_codes')->nullable()->comment('参与码'); // 参与码

            $table->timestamp('start_at')->nullable()->index()->comment('开始时间');
            $table->timestamp('end_at')->nullable()->index()->comment('结束时间');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');


            $table->softDeletes();
            $table->timestamps();
        });


        // 活动奖品
        Schema::create('lottery_skus', function (Blueprint $table) {

            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('activity_id')->index()->nullable()->comment('活动id');

            $table->tinyInteger('sku_type')->default(1)->comment('奖品类型, 1为自定义实物，2为绑定商城商品 3为平台虚拟资产  4为无任何奖品');

            $table->json('assets')->nullable()->comment('当sku_type为2时，绑定的虚拟资产'); 
            $table->integer('product_id')->index()->nullable()->comment('当sku_type为1时, 绑定的商品id');

            $table->string('title', 100)->nullable()->comment('标题'); // 标题
            $table->string('number', 255)->nullable()->comment('编号'); // 副标题
            $table->string('thumb', 255)->nullable()->comment('封面'); // 副标题


            $table->integer('money_price')->unsigned()->nullable()->comment('人民币价格');     // 价格
            $table->integer('score_price')->unsigned()->nullable()->comment('积分价格');     // 价格

            $table->integer('stock')->default(0)->comment('奖品库存'); // 销量
            $table->integer('sales')->default(0)->comment('奖品销量'); // 销量

            $table->integer('per_session_stock')->default(0)->comment('每期奖品库存'); // 销量

            $table->float('odds')->nullable()->comment('中奖几率, 30表示30%');

            $table->integer('level')->default(1)->index()->comment('sku等级 1为一等奖 2为二等奖');

            $table->integer('list_weight')->default(100)->index()->comment('权重');
            $table->tinyInteger('status')->index()->default(1)->nullable()->comment('0已下架 1为已上架');

            $table->softDeletes();
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('lottery_activity');
        Schema::dropIfExists('lottery_skus');
    }
}
